#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: 深圳星河软通科技有限公司 A.Star
# @contact: astar@snowland.ltd
# @site: www.astar.ltd
# @file: cmp .py
# @time: 2020/11/15 18:10
# @Software: PyCharm

from easydict import EasyDict
import gmssl
from gmssl import (
    sm2, sm3, sm4
)
import pysmx
import time
import unittest
from astartool.random import random_string


class TestSM2(unittest.TestCase):
    def setUp(self):
        self.pysmx_sm2 = pysmx.SM2
        self.pk, self.sk = self.pysmx_sm2.generate_keypair()
        self.gmssl_sm2 = sm2.CryptSM2(private_key=self.sk.hex(), public_key=self.pk.hex())

    def test_signverify_100(self):
        ss = random_string(100)
        s = ss.encode()
        K = b'12345678abcdef'
        pysmx_start_time = time.process_time()
        pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
        t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
        pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_sig = self.gmssl_sm2.sign(s, K)
        gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
        gmssl_end_time = time.process_time()
        self.assertTrue(pysmx_ver)
        self.assertTrue(gmssl_ver)
        print("gmssl:", gmssl_sig)
        print("pysmx:", t)
        self.assertEquals(t, gmssl_sig)
        print('100bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    def test_signverify_1000(self):
        ss = random_string(1000)
        s = ss.encode()
        K = b'12345678abcdef'
        pysmx_start_time = time.process_time()
        pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
        t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
        pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_sig = self.gmssl_sm2.sign(s, K)
        gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
        gmssl_end_time = time.process_time()
        self.assertTrue(pysmx_ver)
        self.assertTrue(gmssl_ver)
        print("gmssl:", gmssl_sig)
        print("pysmx:", t)
        self.assertEquals(t, gmssl_sig)
        print('100bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    def test_signverify_10000(self):
        ss = random_string(10000)
        s = ss.encode()
        K = b'12345678abcdef'
        pysmx_start_time = time.process_time()
        pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
        t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
        pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_sig = self.gmssl_sm2.sign(s, K)
        gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
        gmssl_end_time = time.process_time()
        self.assertTrue(pysmx_ver)
        self.assertTrue(gmssl_ver)
        print("gmssl:", gmssl_sig)
        print("pysmx:", t)
        self.assertEquals(t, gmssl_sig)
        print('10000bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)


    def test_signverify_100000(self):
        ss = random_string(100000)
        s = ss.encode()
        K = b'12345678abcdef'
        pysmx_start_time = time.process_time()
        pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
        t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
        pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_sig = self.gmssl_sm2.sign(s, K)
        gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
        gmssl_end_time = time.process_time()
        self.assertTrue(pysmx_ver)
        self.assertTrue(gmssl_ver)
        print("gmssl:", gmssl_sig)
        print("pysmx:", t)
        self.assertEquals(t, gmssl_sig)
        print('100000bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)



if __name__ == '__main__':
    self = EasyDict()
    self.pysmx_sm2 = pysmx.SM2
    self.pk, self.sk = self.pysmx_sm2.generate_keypair()
    self.gmssl_sm2 = sm2.CryptSM2(private_key=self.sk.hex(), public_key=self.pk.hex())

    # 100
    ss = random_string(100)
    print(ss)
    s = ss.encode()
    K = b'12345678abcdef'
    pysmx_start_time = time.process_time()
    pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
    t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
    pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_sig = self.gmssl_sm2.sign(s, K)
    gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
    gmssl_end_time = time.process_time()
    assert (pysmx_ver)
    assert (gmssl_ver)
    print("gmssl:", gmssl_sig)
    print("pysmx:", t)
    assert (t == gmssl_sig)
    print('100bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    # 1000
    ss = random_string(1000)
    print(ss)
    s = ss.encode()
    K = b'12345678abcdef'
    pysmx_start_time = time.process_time()
    pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
    t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
    pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_sig = self.gmssl_sm2.sign(s, K)
    gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
    gmssl_end_time = time.process_time()
    assert (pysmx_ver)
    assert (gmssl_ver)
    print("gmssl:", gmssl_sig)
    print("pysmx:", t)
    assert (t == gmssl_sig)
    print('1000bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    # 10000
    ss = random_string(10000)
    print(ss)
    s = ss.encode()
    K = b'12345678abcdef'
    pysmx_start_time = time.process_time()
    pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
    t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
    pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_sig = self.gmssl_sm2.sign(s, K)
    gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
    gmssl_end_time = time.process_time()
    assert (pysmx_ver)
    assert (gmssl_ver)
    print("gmssl:", gmssl_sig)
    print("pysmx:", t)
    assert (t == gmssl_sig)
    print('10000bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    # 100000
    ss = random_string(100000)
    print(ss)
    s = ss.encode()
    K = b'12345678abcdef'
    pysmx_start_time = time.process_time()
    pysmx_sig = self.pysmx_sm2.Sign(s, self.sk, K, 64)
    t = pysmx_sig.hex()  # 为了统一时间标准，所以把这个加一下hex
    pysmx_ver = self.pysmx_sm2.Verify(pysmx_sig, s, self.pk, 64)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_sig = self.gmssl_sm2.sign(s, K)
    gmssl_ver = self.gmssl_sm2.verify(gmssl_sig, s)
    gmssl_end_time = time.process_time()
    assert (pysmx_ver)
    assert (gmssl_ver)
    print("gmssl:", gmssl_sig)
    print("pysmx:", t)
    assert (t == gmssl_sig)
    print('100000bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)
